package com.qf.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.Exception.MyException;
import com.qf.domain.EbookEntry;
import com.qf.form.test;
import com.qf.service.EbookEntryService;
import com.qf.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Set;
@ResponseBody//发送json RestController
@Controller//
@RequestMapping("/entry")

public class EbookEntryController {
    @Autowired
    private EbookEntryService ebookEntryService;
//@RequestBody 加到接收参数上就变成接收json数据了
//@RequestParam  注解接收方式defaultValue默认值设置
   @GetMapping("/list")//根据获取的信息查询
    public ResultVO list(@RequestParam(defaultValue = "1")Integer page,
                         @RequestParam(value = "limit",defaultValue = "5")Integer size,
                         Integer categoryId) throws JsonProcessingException {
       System.out.println(categoryId);
       ResultVO resultVO=ebookEntryService.findByPage(page,size,categoryId);
        ObjectMapper objectMapper=new ObjectMapper();
       String s = objectMapper.writeValueAsString(resultVO);
       System.out.println(s);
       return resultVO;
    }

    //添加图书
    @PostMapping("/addbook")
    public ResultVO addbook(@Valid EbookEntry ebookEntry, BindingResult bindingResult){
       ResultVO resultVO=new ResultVO();
        System.out.println(ebookEntry.toString());
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            resultVO.setMsg(message);
            resultVO.setCode(400);
            return resultVO;
        }

        ebookEntryService.addbook(ebookEntry);
        return resultVO;

    }


//    @GetMapping(value = "/selectID/{id}")
//    @ResponseBody
//    public ResultVO selectID(@PathVariable  Integer id){
//        if (id==null&&id.equals("")){
//            throw new MyException("您的选择不存在",500);
//        }else {
//            ResultVO resultVO1=ebookEntryService.findId(id);
//            return resultVO1;
//        }
//    }
    @PostMapping(value = "/update")
    public ResultVO updatebook(@Valid EbookEntry ebookEntry ,BindingResult bindingResult){
        System.out.println(ebookEntry);
       ResultVO resultVO=new ResultVO();
       if (bindingResult.hasErrors()){
           String message = bindingResult.getFieldError().getDefaultMessage();
           resultVO.setMsg(message);
           resultVO.setCode(400);
           return resultVO;
       }else {
           ResultVO resultVO1=ebookEntryService.updatebook(ebookEntry);
           return resultVO1;
       }
    }

    @PostMapping(value = "/deletebook")
    public ResultVO deletebook(Integer id){
        ResultVO resultVO=new ResultVO();
        if (id==null){
            throw new MyException("接收不到您要删除的id",500);
        }else {
            int i=ebookEntryService.deletebook(id);
            if (i!=1){
                resultVO.setMsg("删除失败");
                resultVO.setCode(400);
                return resultVO;
            }else {
                return resultVO;
            }
        }


    }




    //测试
    @RequestMapping(value = "/json")
    public String json(@Valid test test, Model model) throws JsonProcessingException {
        ObjectMapper mapper=new ObjectMapper();
        String s = mapper.writeValueAsString(test);//对象转换为json
        com.qf.form.test test1 = mapper.readValue(s, com.qf.form.test.class);//json转换为对象
        Map<String,Integer> map = mapper.readValue(s, Map.class);
        for (Map.Entry entry:map.entrySet()){
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
//        System.out.println(test1.toString());
        model.addAttribute("obj",s);
//        System.out.println(s);
//        System.out.println(test.getUsername());
//        System.out.println(test.getDates());
        return "/WEB-INF/json.jsp";
    }




}
